package com.garry.user.service.impl;

import com.garry.common.security.exception.UsernameExistException;
import com.garry.common.security.util.PasswordUtil;
import com.garry.user.entity.User;
import com.garry.user.repository.UserRepository;
import com.garry.user.service.IUserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

/**
 * Created by GarryGe on 2016/11/30.
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserRepository userRepository;

    @Transactional
    @Override
    public User save(User user) throws Exception {
        User u = userRepository.findByUsername(user.getUsername());
        if (u != null) {
            throw new UsernameExistException(user.getUsername());
        }
        PasswordUtil.encrypt(user);
        return userRepository.save(user);
    }

    @Transactional
    @Override
    public User update(User user) throws Exception {
        return userRepository.save(user);
    }
}
